<template>
	<view class="">
		<u-navbar class="mb-1" title="地址管理" autoBack placeholder></u-navbar>
		<view class="flex align-center border-bottom bg-white my-1 mx-2 p-2" v-for="(item,index) in list" :key="index" style="border-radius: 16rpx;">
			<view class="" style="width: 100%;" @click="choose(item)">
				<!-- 收件人信息 -->
				<view class="">
					<view class="flex align-center ">
					    <u-icon name="map-fill" color="#85bc85"></u-icon>
						<text class="font-30 font-weight-bold ml-1">{{item.name}}</text>
						<view class="text-light6 font-sm ml-2 ">{{item.phone}}</view>
					</view>
				</view>
				<view class="font-28 text-light6 my-2">{{item.Area.province+item.Area.city+item.Area.region+item.detail}}</view>
				<view class="flex justify-between mt-3">
					<!-- 默认 -->
					<view class="flex align-center">
						<u-image :src="item.isdefault==='1'?'/static/select.png':'/static/noselect.png'" width="18" height="18"></u-image>
						<view class="ml-2 font-sm">默认地址</view>
					</view>
					<!-- 删除编辑 -->
					<view class="flex font-28" style="color: #929292;">
						<view class="flex" @click.stop="removeedit(item)">
							<u-icon name="map" class="mr-1"></u-icon>
							删除
						</view>
						<view class="flex ml-3" @click.stop="edit(item)">
							<u-icon name="map" class="mr-1"></u-icon>
							编辑
						</view>
					</view>
				</view>
			</view>
		</view>
		<commonButton class="" title="添加地址" :raduis="16" :margin="'100rpx 22rpx 35rpx'" :background="'#5bb25c'" @click="add"></commonButton>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				list:[],
				isChoose:false,
				
			}
		},
		onLoad(e) {
			this.isChoose = e.type === 'choose'?true:false
			
		},
		onShow() {
			this.getData()
		},
		methods:{
			getData(){
				this.$api.addresslists().then(res=>{
					if(res.code === 1){
						this.list = res.data
					}
				})
			},
			edit(item) {
				this.$set(item,'type','edit')
				this.$set(item,'province',item.Area.province)
				this.$set(item,'city',item.Area.city)
				this.$set(item,'region',item.Area.region)
				uni.$u.route('pages/user/edit', item)
			},
			choose(item){
				console.log('item',item)
				if(this.isChoose){
					// 通知订单提交页修改收货地址
					uni.$emit('choosePath',item)
					// 关闭当前页面
					uni.navigateBack({
						delta: 1
					});
				}
	
			},
			add(){
				uni.$u.route('pages/user/edit', {
					type: 'add'
				})
			},
			choosewxpath(){
				uni.chooseAddress({
					success: (res) => {
						let parmar = {
							provinceName:res.provinceName,
							cityName:res.cityName,
							countyName:res.countyName
						}
						this.$api.wxarea(parmar).then(result=>{
							if(result.code === 1){
								let postObj = {
									name: res.userName,
									phone: res.telNumber,
									province_id: result.data.province_id,
									city_id: result.data.city_id,
									region_id: result.data.region_id,
									detail: res.detailInfo,
									isdefault: '1'
								}
								this.$api.addressadd(postObj).then((resData) => {
									if (resData.code == 1) {
										this.getData()
									}
								})
								
							}
						})
						
						
						
					}
				})
			},
			removeedit(item) {
				uni.showModal({
					title: '提示',
					content: '是否确认删除此地址',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if(res.confirm){
							this.$api.addressdel({
								id: item.address_id
							}).then(result=>{
								if(result.code === 1){
									uni.$u.toast('删除成功')
									this.getData()
								}
							})
						}
					}
				});
			}
		}
	}
</script>

<style>
	.default{
		width: 64rpx;
		height: 40rpx;
		background: #E9FFF7;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		opacity: 1;
		font-size: 20rpx;
		font-family: Source Han Sans CN-Normal, Source Han Sans CN;
		font-weight: 400;
		color: #04BF79;
	}
</style>